<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div [formGroup]="entityFilterFormGroup" class="tb-entity-filter flex flex-col">
  <mat-form-field class="mat-block">
    <mat-label translate>alias.filter-type</mat-label>
    <mat-select required formControlName="type">
      <mat-option *ngFor="let type of aliasFilterTypes" [value]="type">
        {{aliasFilterTypeTranslations.get(type) | translate}}
      </mat-option>
    </mat-select>
    <mat-error *ngIf="entityFilterFormGroup.get('type').hasError('required')">
      {{ 'alias.filter-type-required' | translate }}
    </mat-error>
  </mat-form-field>
  <section class="flex flex-col" [formGroup]="filterFormGroup" [ngSwitch]="entityFilterFormGroup.get('type').value">
    <ng-template [ngSwitchCase]="aliasFilterType.singleEntity">
      <tb-entity-select required
                        useAliasEntityTypes="true"
                        formControlName="singleEntity">
      </tb-entity-select>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityList">
      <tb-entity-type-select required
                             showLabel
                             useAliasEntityTypes="true"
                             [allowedEntityTypes]="listEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
      <tb-entity-list required
                      labelText="{{'entity.entity-list' | translate}}"
                      [entityType]="filterFormGroup.get('entityType').value"
                      formControlName="entityList">
      </tb-entity-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityName">
      <tb-entity-type-select required
                             showLabel
                             useAliasEntityTypes="true"
                             [allowedEntityTypes]="listEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
      <mat-form-field class="mat-block">
        <mat-label translate>entity.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'entity.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="entityNameFilter" required>
        <mat-error *ngIf="filterFormGroup.get('entityNameFilter').hasError('required')">
          {{ 'entity.entity-name-filter-required' | translate }}
        </mat-error>
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityType">
      <tb-entity-type-select required
                             showLabel
                             useAliasEntityTypes="true"
                             [allowedEntityTypes]="listEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.stateEntity">
      <mat-form-field floatLabel="always" class="mat-block">
        <mat-label translate>alias.state-entity-parameter-name</mat-label>
        <input matInput formControlName="stateEntityParamName"
               placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
      </mat-form-field>
      <div class="flex flex-col">
        <label class="tb-small">{{ 'alias.default-state-entity' | translate }}</label>
        <tb-entity-select class="flex-1"
                          useAliasEntityTypes="true"
                          formControlName="defaultStateEntity">
        </tb-entity-select>
      </div>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.assetType">
      <tb-entity-subtype-list required
                              [entityType]="entityType.ASSET"
                              formControlName="assetTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block">
        <mat-label translate>asset.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'asset.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="assetNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.deviceType">
      <tb-entity-subtype-list required
                              [entityType]="entityType.DEVICE"
                              formControlName="deviceTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block">
        <mat-label translate>device.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'device.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="deviceNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityViewType">
      <tb-entity-subtype-list required
                              [entityType]="entityType.ENTITY_VIEW"
                              formControlName="entityViewTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block">
        <mat-label translate>entity-view.name-starts-with</mat-label>
        <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                  matTooltip="{{'entity-view.help-text' | translate }}">help</mat-icon>
        <input matInput formControlName="entityViewNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.edgeType">
      <tb-entity-subtype-list required
                              [entityType]="entityType.EDGE"
                              formControlName="edgeTypes">
      </tb-entity-subtype-list>
      <mat-form-field class="mat-block">
        <mat-label translate>edge.name-starts-with</mat-label>
        <input matInput formControlName="edgeNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.relationsQuery">
      <section class="tb-form-panel no-border no-padding">
        <div class="tb-form-panel stroked no-gap">
          <div class="tb-form-panel-title" translate>alias.root-entity</div>
          <mat-slide-toggle class="mat-slide margin" formControlName="rootStateEntity">
            {{ 'alias.root-state-entity' | translate }}
          </mat-slide-toggle>
          <div *ngIf="filterFormGroup.get('rootStateEntity').value" class="tb-form-panel no-border no-padding no-gap">
            <mat-form-field>
              <mat-label translate>alias.state-entity-parameter-name</mat-label>
              <input matInput formControlName="stateEntityParamName">
            </mat-form-field>
            <tb-entity-select useAliasEntityTypes="true" formControlName="defaultStateEntity">
            </tb-entity-select>
          </div>
          <tb-entity-select *ngIf="!filterFormGroup.get('rootStateEntity').value"
                            required
                            useAliasEntityTypes="true"
                            formControlName="rootEntity">
          </tb-entity-select>
          <div class="tb-form-row no-border no-padding tb-standard-fields">
            <mat-form-field class="flex">
              <mat-label translate>relation.direction</mat-label>
              <mat-select required formControlName="direction">
                <mat-option *ngFor="let type of directionTypes" [value]="type">
                  {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
                </mat-option>
              </mat-select>
            </mat-form-field>
            <mat-form-field class="flex" floatLabel="always">
              <mat-label translate>alias.max-relation-level</mat-label>
              <input matInput
                     type="number"
                     min="1"
                     step="1"
                     placeholder="{{ 'alias.unlimited-level' | translate }}"
                     formControlName="maxLevel">
            </mat-form-field>
          </div>
          <mat-slide-toggle *ngIf="filterFormGroup.get('maxLevel').value > 1 || !filterFormGroup.get('maxLevel').value"
            class="mat-slide" formControlName="fetchLastLevelOnly">
            {{ 'alias.last-level-relation' | translate }}
          </mat-slide-toggle>
        </div>
        <div class="tb-form-panel stroked">
          <div class="tb-form-panel-title" translate>relation.relation-filters</div>
          <tb-relation-filters
            enableNotOption
            [allowedEntityTypes]="allowedEntityTypes"
            formControlName="filters">
          </tb-relation-filters>
        </div>
      </section>
    </ng-template>
    <ng-template [ngSwitchCase]="entityFilterFormGroup.get('type').value === aliasFilterType.assetSearchQuery ||
                                 entityFilterFormGroup.get('type').value === aliasFilterType.deviceSearchQuery ||
                                 entityFilterFormGroup.get('type').value === aliasFilterType.edgeSearchQuery ||
                                 entityFilterFormGroup.get('type').value === aliasFilterType.entityViewSearchQuery ?
                                 entityFilterFormGroup.get('type').value : ''">
      <label class="tb-small">{{ 'alias.root-entity' | translate }}</label>
      <section class="tb-root-state-entity-switch flex flex-row items-center justify-start" style="padding-left: 0;">
        <mat-slide-toggle class="root-state-entity-switch"
                          formControlName="rootStateEntity">
        </mat-slide-toggle>
        <label class="tb-small root-state-entity-label" translate>alias.root-state-entity</label>
      </section>
      <div class="flex flex-1 flex-row" *ngIf="!filterFormGroup.get('rootStateEntity').value">
        <tb-entity-select class="flex-1"
                          required
                          useAliasEntityTypes="true"
                          formControlName="rootEntity">
        </tb-entity-select>
      </div>
      <div class="flex flex-1 flex-col gt-sm:flex-row" *ngIf="filterFormGroup.get('rootStateEntity').value">
        <mat-form-field floatLabel="always" class="mat-block" style="margin-top: 24px; padding-right: 8px;">
          <mat-label translate>alias.state-entity-parameter-name</mat-label>
          <input matInput formControlName="stateEntityParamName"
                 placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
        </mat-form-field>
        <div class="flex flex-1 flex-col">
          <label class="tb-small">{{ 'alias.default-state-entity' | translate }}</label>
          <tb-entity-select class="flex-1"
                            useAliasEntityTypes="true"
                            formControlName="defaultStateEntity">
          </tb-entity-select>
        </div>
      </div>
      <div class="flex flex-1 flex-row">
        <section class="tb-root-state-entity-switch flex flex-row items-center justify-start" style="padding-left: 0;">
          <mat-slide-toggle class="root-state-entity-switch"
                            formControlName="fetchLastLevelOnly">
          </mat-slide-toggle>
          <label class="tb-small root-state-entity-label" translate>alias.last-level-relation</label>
        </section>
      </div>
      <div class="flex flex-1 flex-row gap-2 xs:flex-col">
        <mat-form-field class="mat-block" style="min-width: 100px;">
          <mat-label translate>relation.direction</mat-label>
          <mat-select required formControlName="direction">
            <mat-option *ngFor="let type of directionTypes" [value]="type">
              {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field floatLabel="always" class="mat-block flex-1">
          <mat-label translate>alias.max-relation-level</mat-label>
          <input matInput
                 type="number"
                 min="1"
                 step="1"
                 placeholder="{{ 'alias.unlimited-level' | translate }}"
                 formControlName="maxLevel">
        </mat-form-field>
      </div>
      <div class="mat-caption" style="color: rgba(0,0,0,0.57);" translate>relation.relation-type</div>
      <tb-relation-type-autocomplete
        class="flex-1"
        formControlName="relationType">
      </tb-relation-type-autocomplete>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.assetSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>asset.asset-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.ASSET"
        formControlName="assetTypes">
      </tb-entity-subtype-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.deviceSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>device.device-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.DEVICE"
        formControlName="deviceTypes">
      </tb-entity-subtype-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.edgeSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>edge.edge-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.EDGE"
        formControlName="edgeTypes">
      </tb-entity-subtype-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityViewSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>entity-view.entity-view-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.ENTITY_VIEW"
        formControlName="entityViewTypes">
      </tb-entity-subtype-list>
    </ng-template>
  </section>
</div>
